<?php
    class Menus extends CWidget
    {
        public $menuType;
        
        public function run() {
            $criteria = new CDbCriteria();
            $criteria->select = '*';
            $criteria->order = 'sort';
            $viewName = '';
            switch($this->menuType) {
                case 'article' :
                {
                    $viewName = 'article';
                    $criteria->condition = "status = '".ArticleCategory::STATUS_ACTIVED."'";
                    $data = ArticleCategory::model()->findAll($criteria);
                    break;
                }
                
                case 'product' :
                {
                    $viewName = 'product';
                    $criteria->condition = "status = '".ProductCategory::STATUS_ACTIVED."'";
                    $data = ProductCategory::model()->findAll($criteria);
                    break;
                }
            }
            foreach($data as $value) {
                $menuData['items'][$value['id']] = $value;
                $menuData['parents'][$value['parents']][] = $value['id'];
            }
            if(!empty($menuData)) {
                self::renderMenu($viewName, $menuData);
            }
        }
        
        public function renderMenu($viewName, $menuData) {
            $this->render($viewName, array('menuData'=>$menuData));
        }
        
        public function listCategory($parents, $menuData) {
            $items = '';
            if(isset($menuData['parents'][$parents])) {
                $items .= '<ul>';
                foreach($menuData['parents'][$parents] as $value) {
                    $items .= '<li>';
                    if($menuData['items'][$value]['id'] == $value) {
                        $items .= '<a href="'.$menuData['items'][$value]['id'].'">'.$menuData['items'][$value]['name'].'</a>';
                    }
                    else {
                        $items .= '<a href="'.$menuData['items'][$value]['id'].'">'.$menuData['items'][$value]['name'].'</a>';
                    }
                    $items .= self::listCategory($value, $menuData);
                    $items .= '</li>';
                }
                $items .= '</ul>';
            }
            return $items;
        }
    }
